package org.xbrlapi.fragment.tests;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.Assert;
import org.testng.AssertJUnit;

import java.util.List;

import org.xbrlapi.DOMLoadingTestCase;
import org.xbrlapi.UsedOn;
import org.xbrlapi.utilities.Constants;

/**
 * Tests the implementation of the org.xbrlapi.UsedOn interface.
 * Uses the DOM-based data store to ensure rapid testing.
 *
 * @author Geoffrey Shuetrim (geoff@galexy.net)
 */

public class UsedOnTestCase extends DOMLoadingTestCase {
    private final String STARTING_POINT = "test.data.custom.link.role";

    @BeforeMethod
    protected void setUp() throws Exception {
        super.setUp();
        loader.discover(this.getURI(STARTING_POINT));
    }

    @AfterMethod
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    /**
     * Test analysis of the content of a UsedOn fragment.
     */
    @Test
    public void testUsedOnContentAnalysis() {

        try {
            List<UsedOn> fragments = store.<UsedOn>getXMLResources("UsedOn");
            AssertJUnit.assertTrue(fragments.size() > 0);
            for (UsedOn fragment : fragments) {
                if (fragment.getParent().getDataRootElement().getAttribute("id").equals("newExtendedRoleType")) {
                    AssertJUnit.assertEquals(Constants.XBRL21LinkNamespace, fragment.getUsedOnNamespace());
                    AssertJUnit.assertEquals("presentationLink", fragment.getUsedOnLocalname());
                    //store.serialize(fragment);
                    AssertJUnit.assertTrue(fragment.isUsedOn(Constants.XBRL21LinkNamespace, "presentationLink"));
                }
            }
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    }
}
